package cn.tedu.csmall.product.controller;

import cn.tedu.csmall.product.ex.ServiceException;
import cn.tedu.csmall.product.pojo.dto.AttributeTemplateAddNewDTO;
import cn.tedu.csmall.product.pojo.vo.AttributeTemplateListItemVO;
import cn.tedu.csmall.product.service.IAttributeTemplateService;
import cn.tedu.csmall.product.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "02. 属性模版管理模块")
@RestController
@RequestMapping("/attribute-templates")
@Slf4j
public class AttributeTemplateController {

    @Autowired
    IAttributeTemplateService service;

//    http://localhost:9080/attribute-templates/add-new?name=TestName&pinyin=TestPinyin&keywords=TestKeywords&sort=23
    @ApiOperation("添加属性模板")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    public JsonResult<Void> addNew(@RequestBody AttributeTemplateAddNewDTO attributeTemplateAddNewDTO){
        log.debug("开始处理添加属性模板请求，参数：{}",attributeTemplateAddNewDTO);
        service.addNew(attributeTemplateAddNewDTO);
        return JsonResult.ok();
    }

    //http://localhost:9080/attribute-templates/id值/delete
    @ApiOperation("删除属性模板")
    @ApiOperationSupport(order = 200)
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult<Void> delete(@PathVariable Long id){
        log.debug("开始处理删除属性模板请求，参数：{}",id);
        service.delete(id);
        return JsonResult.ok();
    }
    //http://localhost:9080/attribute-templates
    @ApiOperation("查询属性模板列表")
    @ApiOperationSupport(order = 420)
    @GetMapping("")
    public JsonResult<List<AttributeTemplateListItemVO>> list(){
        log.debug("开始处理查询属性模板列表请求");
        return JsonResult.ok(service.list());
    }
}
